﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyLinksDataPump : ICompanyLinksAccess
    {
        public List<DefaultDataPack> GetLinksList(string copname,int size)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_links
                              where c.c_vc_cpname == copname && !c.c_b_isdelete
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_nvc_title,
                                  c.c_vc_href
                              })
                              .Take(size)
                             .Select(c => new DefaultDataPack() { 
                                TITLE = c.c_nvc_title,
                                URL = c.c_vc_href
                             }).ToList();
                return result;
            }
        }

        public List<DefaultDataPack> GetCompanyMainInfo(string copname, int take)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_maininfo
                              where c.c_vc_cpname == copname
                              orderby c.c_dt_date descending
                              select new { c.c_vc_link, c.c_vc_photo, c.c_vc_title })
                             .Take(take).ToList();
                if (result.Any())
                {
                    return result.Select(c => new DefaultDataPack()
                    {
                        INFOLINK = c.c_vc_link,
                        PHOTOTITLE = c.c_vc_title,
                        INFOPHOTO = c.c_vc_photo
                    }).ToList();
                }
            }
            return list;
        }


        public List<DefaultDataPack> GetCompanyMainInfoItem(string copname, int bid)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            //using (mysql_companyEntities ctx = new mysql_companyEntities())
            //{
            //    var result = (from c in ctx.tb_maininfo
            //                  where c.c_vc_cpname == copname && c.c_i_id ==
            //                    (from x in ctx.tb_business
            //                     where x.c_i_id == bid
            //                     select x.c_i_refences).FirstOrDefault()
            //                  orderby c.c_dt_date descending
            //                  select new { c.c_vc_link, c.c_vc_photo, c.c_vc_title, c.c_i_id })
            //                 .ToList();
            //    if (result.Any())
            //    {
            //        return result.Select(c => new DefaultDataPack()
            //        {
            //            INFOLINK = c.c_vc_link,
            //            PHOTOTITLE = c.c_vc_title,
            //            INFOPHOTO = c.c_vc_photo,
            //            INFOID = c.c_i_id.ToString()
            //        }).ToList();
            //    }
            //}
            return list;
        }


        public int EditCompanyMainInfoItem(int id, string copname, bool isMain, string photo, string title, string link, ref int refencesId)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                if (isMain)
                {
                    var item = (from c in ctx.tb_maininfo
                                where c.c_vc_cpname == copname && c.c_i_id == id
                                select c).FirstOrDefault();
                    if (item != null)
                    {
                        item.c_vc_link = link;
                        item.c_vc_photo = photo;
                        item.c_vc_title = title;
                        refencesId = id;
                        return ctx.SaveChanges();
                    }
                }
                else
                {
                    var item = (from c in ctx.tb_maininfo
                                where c.c_vc_cpname == copname && c.c_i_id == id
                                select c).FirstOrDefault();
                    if (item != null)
                    {
                        ctx.tb_maininfo.DeleteObject(item);
                        refencesId = -1;
                        return ctx.SaveChanges();
                    }
                }
                return 0;
            }
        }
    }
}
